//
//  Picture.m
//  project-A (pianke)
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "Picture.h"

#import "firstViewConrtoller.h"
@interface Picture ()<UIScrollViewDelegate>


@end

@implementation Picture

- (void)dealloc {
    [_scroll release];
    [_page release];
    [_arrImages release];
    [_imageview release];
    [_button release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame images:(NSArray<NSString *> *)images {
    self = [super initWithFrame:frame];
    if (self) {
        self.arrImages = [NSMutableArray arrayWithArray:images];
        //创建子控件
       [self createScrollWithFrame:frame];
    }
    return self;
}

#pragma mark - UIScrollView
- (void)createScrollWithFrame:(CGRect)frame {
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [self addSubview:self.scroll];
    [_scroll release];
    self.scroll.backgroundColor = [UIColor lightGrayColor];
    
    //api
    //设置contentSize
    self.scroll.contentSize = CGSizeMake(frame.size.width * self.arrImages.count, frame.size.height);
    //scorllView上添加imageView对象
    [self createImageViewWithFrame:frame];
    
    
    //开启翻页效果
    self.scroll.pagingEnabled = YES;
    //回弹效果
    self.scroll.bounces = NO;
    //指定代理人
    self.scroll.delegate = self;
    
   }
- (void)createImageViewWithFrame:(CGRect)frame {
    for (int i = 0; i < self.arrImages.count; i++) {
        self.imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.arrImages[i]]];
        self.imageview.frame = CGRectMake(frame.size.width * i, -30, frame.size.width, frame.size.height + 30);
        
        self.imageview.userInteractionEnabled = YES;
        
        if (i == 4) {
            self.button = [[UIButton alloc]initWithFrame:CGRectMake(117, 578, frame.size.width - 234, 46)];
            
            self.button.backgroundColor = [UIColor whiteColor];
            
            [self.imageview addSubview:self.button];
            
           self.button.layer.cornerRadius = 6;
            [self.button setTitle:@"开启全新片刻" forState:UIControlStateNormal];
            
          self.button.titleLabel.font = [UIFont systemFontOfSize: 25];
            [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
           self.button.layer.borderWidth = 2;
            self.button.layer.borderColor = [UIColor grayColor].CGColor;
            [self.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
            
            [self.button release];
            
            
        }
        [self.scroll addSubview:self.imageview];
        [self.imageview release];
      }
   }

-(void)buttonAction{
    
       
    
    
    
}

#pragma mark - UIScrollView 协议方法
//当滑动内容视图时
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
}

//当减速已经完成时
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    self.page.currentPage = scrollView.contentOffset.x / self.frame.size.width;
    
}

@end
